﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using Mozilla.Connection;
using Mozilla.Documents;

namespace ConsoleTesterApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            
            MozillaListener testListen = new MozillaListener(4242,"ChaCha");
            testListen.Mozilla.Windows.WindowsAdded += new Mozilla.Windows.WindowCollection.WindowsAddedDelegate(Windows_WindowsAdded);
            testListen.Mozilla.Windows.WindowRemoved += new Mozilla.Windows.WindowCollection.WindowRemovedDelegate(Windows_WindowRemoved);
            testListen.EventsRegisteredEvent += new MozillaListener.EventsRegisteredDelegate(testListen_EventsRegisteredEvent);
            testListen.DocumentClosingEvent += new MozillaListener.DocumentClosingDelegate(testListen_DocumentClosingEvent);
            Thread RunEvents = new Thread(new ThreadStart(DisplayEvents));
            RunEvents.IsBackground = true;
            RunEvents.Start();
            while (!Console.KeyAvailable)
            {
                System.Threading.Thread.Sleep(1000);
            }
        }

        static void testListen_DocumentClosingEvent(string WindowID, string DocumentID)
        {
            eventReceived.Add(string.Format("Tab {0} in window {1} closed", DocumentID, WindowID));
        }

        static void testListen_EventsRegisteredEvent(System.Text.RegularExpressions.MatchCollection EventsMatch)
        {
            foreach (System.Text.RegularExpressions.Match eventr in EventsMatch)
            {
                eventReceived.Add(eventr.Value);
            }
        }

        static void Windows_WindowRemoved(string WindowID)
        {
            eventReceived.Add(string.Format("Window Closed ID : {0}", WindowID));
        }

        static void Windows_WindowsAdded(Mozilla.Windows.Window Window)
        {
            eventReceived.Add(string.Format("Window Added ID : {0}", Window.ID));
            Window.Documents.DocumentAdded += new DocumentCollection.DocumentAddedDelegate(Documents_DocumentAdded);
        }

        static void Documents_DocumentAdded(Document Document)
        {
            eventReceived.Add(string.Format("Document Added ID : {0}", Document.ID));
        }

        static void Window_WindowUpdated(Mozilla.Documents.Document document)
        {
        }

        static System.Collections.Specialized.StringCollection eventReceived;

        static void DisplayEvents()
        {
            eventReceived = new System.Collections.Specialized.StringCollection();
            while (true == true)
            {
                if (eventReceived.Count != 0)
                {
                    Console.WriteLine(eventReceived[0]);
                    eventReceived.RemoveAt(0);
                    continue;
                }
                Thread.Sleep(300);
            }
        }
    }
}
